package com.specificgroup.jira.plugin.acpro.action;

import com.atlassian.plugin.webresource.WebResourceManager;
import com.specificgroup.jira.plugin.acpro.model.Settings;
import com.specificgroup.jira.plugin.acpro.model.jira.JiraProject;
import com.specificgroup.jira.plugin.acpro.service.JiraAcproPluginJiraManager;
import com.specificgroup.jira.plugin.acpro.session.SessionUtil;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ivan Holub
 */
public class SelectJiraProjectAction extends AcproActionSupport {

    private static final String NEXT_ACTION = "AcproItemNumberCustomField!default.jspa";

    private static final String RESOURCE_KEY = "com.specificgroup.jira.plugin.acpro.jira-acpro-plugin:acpro-resource-js-select-jira-project";

    private static final String ID_SEPARATOR = " ";

    private List<JiraProject> jiraProjects;

    private WebResourceManager webResourceManager;

    private String projectId;

    private Long projectIdLong;

    private String projectComponentsIds;

    private JiraAcproPluginJiraManager jiraAcproPluginJiraManager;

    public void setProjectComponentsIds(String projectComponentsIds) {
        this.projectComponentsIds = projectComponentsIds;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public SelectJiraProjectAction(JiraAcproPluginJiraManager jiraAcproPluginJiraManager, WebResourceManager webResourceManager) {
        this.jiraAcproPluginJiraManager = jiraAcproPluginJiraManager;
        this.webResourceManager = webResourceManager;
    }

    public List<JiraProject> getJiraProjects() {
        return jiraProjects;
    }

    @Override
    public String doDefault() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        webResourceManager.requireResource(RESOURCE_KEY);
        
        jiraProjects = jiraAcproPluginJiraManager.getJiraProjects();

        return super.doDefault();
    }

    @Override
    protected String doExecute() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        Settings settings = SessionUtil.getSettings();
        settings.setJiraProjectId(projectIdLong);

        if (StringUtils.isNotEmpty(projectComponentsIds)) {
            List<Long> jiraProjectComponentsIdsLong = new ArrayList<Long>();
            String [] ids = projectComponentsIds.split(ID_SEPARATOR);
            for (String idStr: ids) {
                Long id = Long.valueOf(idStr);
                jiraProjectComponentsIdsLong.add(id);
            }
            settings.setJiraProjectComponentsIds(jiraProjectComponentsIdsLong);            
        }

        String redirect = getRedirect(NEXT_ACTION);
        return redirect;
    }

    @Override
    protected void doValidation() {

        boolean isProjectValid = false;
        List<JiraProject> projects = jiraAcproPluginJiraManager.getJiraProjects();
        if (StringUtils.isNotEmpty(projectId)) {
            JiraProject p = findProject(projects, Long.valueOf(projectId));
            if (p != null) {
                isProjectValid = true;
                projectIdLong = p.getId();
            }
        }

        if (!isProjectValid) {

            webResourceManager.requireResource(RESOURCE_KEY);

            jiraProjects = jiraAcproPluginJiraManager.getJiraProjects();

            addErrorMessage(getText("error.project"));
        }

        super.doValidation();
    }

    private JiraProject findProject(List<JiraProject> projects, Long id) {
        for (JiraProject p: projects) {
            if (id.equals(p.getId())) {
                return p;
            }
        }
        return null;
    }
}
